package com.eoe.wiki.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.eoe.wiki.db.DatabaseColumn;
import com.eoe.wiki.utils.SqliteWrapper;

public class GeneralDao<T extends DatabaseColumn> {

	public T tableClass;
	public Context context;
	public Uri uri;
	public GeneralDao(T tableClass, Context context) {
		super();
		this.tableClass = tableClass;
		this.context = context;
		uri = Uri.parse("content://" + DatabaseColumn.AUTHORITY + "/" + this.tableClass.getTableName());
	}
	
	public Cursor queryAll(long id){
		return SqliteWrapper.query(context, context.getContentResolver(), uri.withAppendedPath(uri, String.valueOf(id)), this.tableClass.getTableColumns(), null,null, null);
	}	
	
	public Cursor queryByParameter(String name,long value){
		return SqliteWrapper.query(context, context.getContentResolver(), uri, this.tableClass.getTableColumns(), name + "='" + value + "'", null, null);
	}
	
	public Cursor queryByParameter(String name,String value){
		return SqliteWrapper.query(context, context.getContentResolver(), uri, this.tableClass.getTableColumns(), name + "='" + value + "'", null, null);
	}
	
	public Uri insert(ContentValues values){
		return SqliteWrapper.insert(context, context.getContentResolver(), uri, values);
	}
	
	public int delete(long id){
		return SqliteWrapper.delete(context, context.getContentResolver(), uri, BaseColumns._ID + "=" + id, null);
	}
	
	public int update(ContentValues values){
		long id = values.getAsLong(BaseColumns._ID);
		if(id>=0){
			return SqliteWrapper.update(context, context.getContentResolver(), uri, values, BaseColumns._ID + "=" + id, null);
		}
		return 0;
	}
}
